/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-06-20 22:36:41
 * @LastEditTime: 2022-06-20 23:11:18
 * @LastEditors: liutq
 * @Reference:
 */
package notice

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func NoticeDingTalk(msg string, url string) {
	dingUrl := "https://oapi.dingtalk.com/robot/send?access_token=dbc8f2d95c19ebc8512ef0d453b1466325f80026ec2baf10d03af8efd8515507"
	msgText := ""
	if url != "" {
		msgText = fmt.Sprintf("### 服务端程序异常通知： \n %s\n\n [点击这里快速设置](%s)", msg, url)
	} else {
		msgText = fmt.Sprintf("### 服务端程序异常通知： \n %s", msg)
	}

	content, data := make(map[string]string), make(map[string]interface{})
	content["title"] = "钉钉消息推送"
	content["text"] = msgText
	data["msgtype"] = "markdown"
	data["markdown"] = content
	b, _ := json.Marshal(data)

	resp, err := http.Post(dingUrl,
		"application/json",
		bytes.NewBuffer(b))
	if err != nil {
		fmt.Println(err)
	}
	defer resp.Body.Close()
	ioutil.ReadAll(resp.Body)
}
